package com.efivestar.idm.api.service;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efivestar.idm.api.common.Cons;
import com.efivestar.idm.api.domain.DeviceDomain;
import com.efivestar.idm.api.repository.DeviceMapper;

@Service
public class DeviceService {

    Logger logger = LoggerFactory.getLogger(DeviceService.class);


    @Autowired
    private DeviceMapper deviceMapper;
    
    /**
     * 根据设备ID获取设备.
     * @param deviceId
     * @param DeviceDomain
     */
    public DeviceDomain getDeviceById(String  deviceId) {
        return deviceMapper.selectByPrimaryKey(deviceId);
    }
    
    /**
     * 添加设备.
     * @param deviceDomain
     * @param int
     */
    public int addDevice(DeviceDomain deviceDomain) {
        DeviceDomain dbDevice = deviceMapper.selectByPrimaryKey(deviceDomain.getDeviceId());
        if (dbDevice == null) {
            deviceDomain.setDefenseStatus(Cons.DEFENSE_OFF_STATUS);
            return deviceMapper.insert(deviceDomain);
        }
        return 0;
    }


    /**
     * 更新电量.
     * @param deviceId
     * @param power
     */
    public void updatePower(String deviceId, int power) {
        DeviceDomain deviceDomain = DeviceDomain.builder().deviceId(deviceId).power(power).heartTime(new Date()).build();
        deviceMapper.updateByPrimaryKeySelective(deviceDomain);
    }
    
    /**
     * 更新安防状态.
     * @param deviceId
     * @param defenseStatus
     */
    public void updateDefenseStatus(String deviceId, int defenseStatus) {
        DeviceDomain deviceDomain = DeviceDomain.builder().deviceId(deviceId).defenseStatus(defenseStatus).heartTime(new Date()).build();
        deviceMapper.updateByPrimaryKeySelective(deviceDomain);
    }

    /**
     * 更新电量和撤防状态.
     * @param deviceId
     * @param power
     */
    public void updatePowerAndDefenseStatus(String deviceId, int power, int defenseStatus) {
        DeviceDomain deviceDomain = DeviceDomain.builder().deviceId(deviceId).power(power).defenseStatus(defenseStatus).heartTime(new Date()).build();
        deviceMapper.updateByPrimaryKeySelective(deviceDomain);
    }

}



